﻿using System;
using System.Collections.Generic;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Text;
using MINERP.COM;
using MINERP.DAL.InfoConfig;
using MINERP.MODEL.InfoConfig;
using MINERP.DAL;
using MINERP.MODEL.FoundationModel;
using System.Data;
using System.Windows.Forms;



namespace MINERP.BLL.InfoConfig
{
    public class Shop_New_BLL
    {
        /// <summary>
        /// 得到所有商户列表
        /// </summary>
        /// <returns></returns>
        public List<Shop_New_Info> GetAllShopList()
        {
            List<Shop_New_Info> rtnList = new List<Shop_New_Info>();
            List<Shop> shopList = new List<Shop>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                shopList = db.Shop.Where(sh=>sh.Status==1).ToList();
                rtnList = ConvertShop_New_InfoFromShopEntity(shopList, db);
            }

            return rtnList;
        }

        /// <summary>
        /// 通过助记码查找商户
        /// </summary>
        /// <returns></returns>
        public List<Shop_New_Info> GetShopListByMnemonicCode(string mnemonicCode)
        {
            List<Shop_New_Info> rtnList = new List<Shop_New_Info>();
            List<Shop> shopList = new List<Shop>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                shopList = db.Shop.Where(sh => sh.Status == 1&&sh.mnemonicCode.Contains(mnemonicCode)).ToList();
                rtnList = ConvertShop_New_InfoFromShopEntity(shopList, db);
            }

            return rtnList;
        }
        /// <summary>
        /// 检查商户是否重复
        /// </summary>
        /// <param name="inShopName"></param>
        /// <returns>true 重复 false 不重复</returns>
        public bool CheckShopName(string inShopName)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                Shop shop = db.Shop.FirstOrDefault(p => p.shopName == inShopName);
                if (shop != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
        }
        /// <summary>
        /// 获取所有的纸型
        /// </summary>
        /// <returns></returns>
        public List<PaperType_Info> GetAllPaperType()
        {
            List<PaperType_Info> rtnList = new List<PaperType_Info>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<paperType> listType = db.paperType.ToList();
                foreach (paperType type in listType)
                {
                    PaperType_Info info = new PaperType_Info();
                    info.Name = type.Name;
                    info.PaperTypeID = type.PaperTypeID;
                    rtnList.Add(info);
                }
            }
            return rtnList;
        }
        /// <summary>
        /// 获取所有纸面
        /// </summary>
        /// <returns></returns>
        public List<PaperFace_Info> GetAllPaperFace()
        {
            List<PaperFace_Info> rtnList = new List<PaperFace_Info>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<paperFace> listType = db.paperFace.Where(p=>p.Status ==1).ToList();
                foreach (paperFace type in listType)
                {
                    PaperFace_Info info = new PaperFace_Info();
                    info.Name = type.Name;
                    info.PaperFaceID = type.PaperFaceID;
                    rtnList.Add(info);
                }
            }
            return rtnList;
        }
        /// <summary>
        /// 新添加商户
        /// </summary>
        /// <param name="inNewInfo"></param>
        /// <returns></returns>
        public int AddNewShop(Shop_New_Info inNewInfo)
        {
            if (!CheckShopName(inNewInfo.ShopName))//检查商户是否重复
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    Shop shopAdd = new Shop();
                    shopAdd.address = inNewInfo.Addr;
                    shopAdd.Area = inNewInfo.AreaName;
                    shopAdd.EmailAddr = inNewInfo.Email;
                    shopAdd.mnemonicCode = inNewInfo.MiniCode;
                    shopAdd.MSN = inNewInfo.MSN;
                    shopAdd.PaperFaceId = inNewInfo.PaperFaceID;
                    shopAdd.PaperTypeId = inNewInfo.PaperTypeID;
                    shopAdd.Priority = inNewInfo.Priority;
                    shopAdd.QQ = inNewInfo.QQ;
                    shopAdd.Remark = inNewInfo.Remark;
                    shopAdd.Salesman = inNewInfo.SalesMan;
                    shopAdd.shopKeeper = inNewInfo.ContactPerson;
                    shopAdd.shopName = inNewInfo.ShopName;
                    shopAdd.Tel1 = inNewInfo.MobilePhoneNum;
                    shopAdd.Tel2 = inNewInfo.PhoneNum;

                    shopAdd.CreditLimit = 0;
                    shopAdd.Balance = 0;
                    shopAdd.OrderBalance = 0;
                    shopAdd.Status = 1;
                    shopAdd.IsNeedPrintUPrice = inNewInfo.IsPrintPrice;
                    shopAdd.AreaPrice = 0;
                    shopAdd.XmlVersion = 0;
                    shopAdd.CreditLevel = inNewInfo.CreditLevel;
                    shopAdd.IsAssignByCustom = inNewInfo.IsAssignByCustom;
                    shopAdd.SHACODE = "";
                    shopAdd.ForceCheck = false;
                    shopAdd.ClientVersion = 0.0;
                    shopAdd.LastPrintDate = DateTime.Now;

                    List<paperType> ListType = db.paperType.ToList();
                    shopAdd.Shop_Paper_Price = new EntityCollection<Shop_Paper_Price>();
                    List<Shop_Paper_Price> listPrice = new List<Shop_Paper_Price>();
                    foreach (paperType list in ListType)
                    {
                        Shop_Paper_Price paperPrice = new Shop_Paper_Price();
                        paperPrice.paperType = list;
                        paperPrice.Shop = shopAdd;
                        paperPrice.UnitPrice = 0;
                        listPrice.Add(paperPrice);
                    }

                    List<CommonChargeItem> listcommom = db.CommonChargeItem.ToList();
                    // shopAdd.Shop_CommonChargeItem_Price=new EntityCollection<Shop_CommonChargeItem_Price>();
                    List<Shop_CommonChargeItem_Price> listShopComm = new List<Shop_CommonChargeItem_Price>();
                    foreach (CommonChargeItem item in listcommom)
                    {
                        Shop_CommonChargeItem_Price sccip = new Shop_CommonChargeItem_Price();
                        sccip.CommonChargeItem = item;
                        sccip.Shop = shopAdd;
                        item.tb_CostTypeReference.Load();
                        sccip.CostTypeId = item.tb_CostType.CostTypeId;
                        sccip.AreaPrice = item.AreaPrice;
                        sccip.SinglePrice = item.SinglePrice;
                        sccip.UnitPrice = item.UnitPrice;
                    }
                    shop_DistributionInfo shopDistributionInfo = new shop_DistributionInfo();
                    shopDistributionInfo.BusTimetable = "";
                    shopDistributionInfo.ConsigneeName = "";
                    shopDistributionInfo.ConsigneeTelePhone = "";
                    shopDistributionInfo.DistributionAddress = "";
                    shopDistributionInfo.DistributionMode = "";
                    shopDistributionInfo.FarePayer = "";
                    shopDistributionInfo.ReMark = "";
                    shopDistributionInfo.Shop = shopAdd;
                    db.AddToshop_DistributionInfo(shopDistributionInfo);

                    //shopAdd.Shop_Paper_Price = listPrice;
                    db.AddToShop(shopAdd);
                    db.SaveChanges();
                    //shopAdd.FtpLoginName = shopAdd.ShopId.ToString();
                    //db.SaveChanges();
                    return 0;
                }

            }
            else
            {
                return -1;//商户名称重复
            }

        }
        /// <summary>
        /// 修改商户信息
        /// </summary>
        /// <param name="inUpdateInfo"></param>
        /// <returns></returns>
        public int UpdateShop(Shop_New_Info inUpdateInfo)
        {

            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                Shop shopUpd = db.Shop.FirstOrDefault(p => p.ShopId == inUpdateInfo.ShopID);
                if (shopUpd == null)
                {
                    return -1;
                }
                Shop CheckShop = db.Shop.FirstOrDefault(p => p.shopName == inUpdateInfo.ShopName);
                if (CheckShop != null && shopUpd.ShopId != CheckShop.ShopId)
                {
                    return -2;//已经存在相同的商户名称
                }
                shopUpd.address = inUpdateInfo.Addr;
                shopUpd.Area = inUpdateInfo.AreaName;
                shopUpd.EmailAddr = inUpdateInfo.Email;
                shopUpd.mnemonicCode = inUpdateInfo.MiniCode;
                shopUpd.MSN = inUpdateInfo.MSN;
                shopUpd.PaperFaceId = inUpdateInfo.PaperFaceID;
                shopUpd.PaperTypeId = inUpdateInfo.PaperTypeID;
                shopUpd.Priority = inUpdateInfo.Priority;
                shopUpd.QQ = inUpdateInfo.QQ;
                shopUpd.Remark = inUpdateInfo.Remark;
                shopUpd.Salesman = inUpdateInfo.SalesMan;
                shopUpd.shopKeeper = inUpdateInfo.ContactPerson;
                shopUpd.shopName = inUpdateInfo.ShopName;
                shopUpd.Tel1 = inUpdateInfo.MobilePhoneNum;
                shopUpd.Tel2 = inUpdateInfo.PhoneNum;

                shopUpd.Status = 1;
                shopUpd.IsNeedPrintUPrice = inUpdateInfo.IsPrintPrice;
                shopUpd.IsAssignByCustom = inUpdateInfo.IsAssignByCustom;
                shopUpd.CreditLevel = inUpdateInfo.CreditLevel;

                //shopAdd.

                db.SaveChanges();
                return 0;
            }



        }
        /// <summary>
        /// 删除商户
        /// </summary>
        /// <param name="inShopId"></param>
        /// <returns></returns>
        public int DelShop(int inShopId)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                Shop shopUpd = db.Shop.FirstOrDefault(p => p.ShopId == inShopId);
                if (shopUpd == null)
                {
                    return -1;
                }
                shopUpd.Status = 0;//不可用
                db.SaveChanges();
                return 0;
            }
        }

        /// <summary>
        /// 获取纸型价格表
        /// 
        /// </summary>
        /// <param name="inShopId"></param>
        /// <returns></returns>
        public List<Shop_Paper_Price_Info> GetPaperPriceByShopId(int inShopId)
        {
            List<Shop_Paper_Price_Info> rtnList = new List<Shop_Paper_Price_Info>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                Shop shop = db.Shop.FirstOrDefault(p => p.ShopId == inShopId);
                shop.Shop_Paper_Price.Load();
                List<Shop_Paper_Price> list = shop.Shop_Paper_Price.ToList();
                List<paperType> paperTypes = db.paperType.ToList();
                foreach (paperType paperType in paperTypes)
                {
                    //foreach (Shop_Paper_Price shopPaperPrice in list)
                    //{
                    //    Shop_Paper_Price_Info shopPaperPriceInfo = new Shop_Paper_Price_Info();
                    //    //shopPaperPriceInfo.Shop = new Shop_New_Info() { ShopID = shop.ShopId, ShopName = shop.shopName };
                    //    shopPaperPrice.paperTypeReference.Load();
                    //    paperType type = shopPaperPrice.paperType;
                    //    //shopPaperPriceInfo.PaperType = new PaperType_Info() { PaperTypeID = type.PaperTypeID, Name = type.Name };

                    //    shopPaperPriceInfo.UnitPrice = shopPaperPrice.UnitPrice;
                        
                    //    rtnList.Add(shopPaperPriceInfo);
                    //}
                    Shop_Paper_Price_Info shopPaperPriceInfo=new Shop_Paper_Price_Info();
                    Shop_Paper_Price shopPaperPrice = list.FirstOrDefault(p => p.PaperTypeId == paperType.PaperTypeID);

                    if(shopPaperPrice!=null)
                    {
                        shopPaperPriceInfo.ShopInfo = new Shop_New_Info(){ShopID =inShopId,ShopName = shop.shopName};                      
                        shopPaperPriceInfo.isShopPaperPrice = shopPaperPriceInfo.isShopPaperPrice2 = true;
                        shopPaperPriceInfo.UnitPrice = shopPaperPrice.UnitPrice;
                        shopPaperPriceInfo.PaperType=new PaperType_Info(){PaperTypeID = paperType.PaperTypeID,Name = paperType.Name};
                        //shopPaperPriceInfo
                        
                    }
                    else
                    {
                        shopPaperPriceInfo.ShopInfo = null;
                        shopPaperPriceInfo.isShopPaperPrice = shopPaperPriceInfo.isShopPaperPrice2 = false;
                        shopPaperPriceInfo.UnitPrice = 0;
                        shopPaperPriceInfo.PaperType = new PaperType_Info() { PaperTypeID = paperType.PaperTypeID, Name = paperType.Name };
                    }
                    rtnList.Add(shopPaperPriceInfo);
                }
               


            }
            return rtnList;
        }
        /// <summary>
        /// 更改纸型价格表
        /// </summary>
        /// <param name="inList"></param>
        /// <returns></returns>
        public int UpdateShopPaperType_Price(List<Shop_Paper_Price_Info> inList,int inShopID)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {

              
                foreach (Shop_Paper_Price_Info shopPaperPriceInfo in inList)
                {
                    //Shop_Paper_Price spp =
                    //    db.Shop_Paper_Price.FirstOrDefault(
                    //        p =>
                    //        p.ShopId == shopPaperPriceInfo.ShopInfo.ShopID &&
                    //        p.PaperTypeId == shopPaperPriceInfo.PaperType.PaperTypeID);

                    //spp.UnitPrice = shopPaperPriceInfo.UnitPrice;
                    if(shopPaperPriceInfo.isShopPaperPrice&&shopPaperPriceInfo.isShopPaperPrice2)//原有纸型 修改价格
                    {
                        Shop_Paper_Price spp =
                            db.Shop_Paper_Price.FirstOrDefault(
                                p =>
                                p.ShopId == shopPaperPriceInfo.ShopInfo.ShopID &&
                                p.PaperTypeId == shopPaperPriceInfo.PaperType.PaperTypeID);
                        if (spp != null)
                        {
                            spp.UnitPrice = shopPaperPriceInfo.UnitPrice;
                        }
                        else
                        {
                            Shop inShop = db.Shop.FirstOrDefault(p => p.ShopId == inShopID);
                            Shop_Paper_Price sppa = new Shop_Paper_Price();
                            sppa.Shop = inShop;
                            sppa.paperType = db.paperType.FirstOrDefault(p => p.Name == shopPaperPriceInfo.PaperType.Name);
                            sppa.UnitPrice = shopPaperPriceInfo.UnitPrice;
                            inShop.Shop_Paper_Price.Load();
                           
                             inShop.Shop_Paper_Price.Add(sppa);
                             inShop.ProductVersion += 0.1;
                        }
                    }
                    else if (!shopPaperPriceInfo.isShopPaperPrice && shopPaperPriceInfo.isShopPaperPrice2)//删除的纸型
                    {
                        Shop inShop = db.Shop.FirstOrDefault(p=>p.ShopId == shopPaperPriceInfo.ShopInfo.ShopID);
                        Shop_Paper_Price spp =
                           db.Shop_Paper_Price.FirstOrDefault(
                               p =>
                               p.ShopId == shopPaperPriceInfo.ShopInfo.ShopID &&
                               p.PaperTypeId == shopPaperPriceInfo.PaperType.PaperTypeID);
                        if (spp != null)
                        {
                            inShop.Shop_Paper_Price.Load();
                            if (inShop.Shop_Paper_Price != null)
                            {
                                inShop.Shop_Paper_Price.Remove(spp);
                            }
                        }
                        inShop.ProductVersion += 0.1;
                        
                    }
                    else if (shopPaperPriceInfo.isShopPaperPrice && !shopPaperPriceInfo.isShopPaperPrice2)//新添加的纸型
                    {
                        Shop inShop = db.Shop.FirstOrDefault(p => p.ShopId == inShopID);
                        Shop_Paper_Price spp=new Shop_Paper_Price();
                        spp.Shop = inShop;
                        spp.paperType = db.paperType.FirstOrDefault(p => p.Name == shopPaperPriceInfo.PaperType.Name);
                        spp.UnitPrice = shopPaperPriceInfo.UnitPrice;
                        inShop.Shop_Paper_Price.Load();
                        Shop_Paper_Price sppE = inShop.Shop_Paper_Price.FirstOrDefault(p => p.PaperTypeId == spp.paperType.PaperTypeID);
                        if (sppE != null)
                        {
                            sppE.UnitPrice = shopPaperPriceInfo.UnitPrice;
                        }
                        else
                        {
                            inShop.Shop_Paper_Price.Add(spp);
                        }
                        inShop.ProductVersion += 0.1;
                    }
                    else
                    {
                        
                    }

                   
                }
                db.SaveChanges();
               
                return 0;
            }
        }

        /// <summary>
        /// 获取商户基本收费类型
        /// </summary>
        /// <param name="inShopId"></param>
        /// <returns></returns>
        public List<Shop_CommonChargeItem_Price_Info> GetShopCommonChangeItemPriceInfoByShopId(int inShopId)
        {
            List<Shop_CommonChargeItem_Price_Info> rtnList = new List<Shop_CommonChargeItem_Price_Info>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                Shop shop = db.Shop.FirstOrDefault(p => p.ShopId == inShopId);
                shop.Shop_CommonChargeItem_Price.Load();
                List<Shop_CommonChargeItem_Price> listSccip = shop.Shop_CommonChargeItem_Price.ToList();
                //foreach (Shop_CommonChargeItem_Price list in listSccip)
                //{
                //    Shop_CommonChargeItem_Price_Info info = new Shop_CommonChargeItem_Price_Info();
                //    info.AreaPrice = list.AreaPrice;
                //    list.CommonChargeItemReference.Load();
                //    list.CommonChargeItem.tb_CostTypeReference.Load();
                //    info.CCII = new CommonChargeItem_info() { AreaPrice = list.CommonChargeItem.AreaPrice, CommonChargeItemId = list.CommonChargeItem.CommonChargeItemId, CostTypeId = list.CommonChargeItem.tb_CostType.CostTypeId, ItemName = list.CommonChargeItem.ItemName, SinglePrice = list.CommonChargeItem.SinglePrice, UnitPrice = list.CommonChargeItem.UnitPrice };
                //    info.CostType = new tb_CostType_info() { CostTypeId = list.CommonChargeItem.tb_CostType.CostTypeId, Remark = list.CommonChargeItem.tb_CostType.Remark, TypeName = list.CommonChargeItem.tb_CostType.TypeName };
                //    info.Shop = new Shop_New_Info() { ShopID = shop.ShopId, ShopName = shop.shopName };
                //    info.SinglePrice = list.SinglePrice;
                //    info.UnitPrice = list.UnitPrice;
                //    info.TypeName = info.CostType.TypeName;
                //    rtnList.Add(info);
                //}
                List<CommonChargeItem> listCommonItems = db.CommonChargeItem.ToList();
                foreach (CommonChargeItem list in listCommonItems)
                {
                    Shop_CommonChargeItem_Price sccip =
                        listSccip.FirstOrDefault(p => p.CommonChargeItemId == list.CommonChargeItemId && p.ShopId == inShopId);
                    Shop_CommonChargeItem_Price_Info info = new Shop_CommonChargeItem_Price_Info();
                    if(sccip!=null)
                    {
                        info.AreaPrice = sccip.AreaPrice;
                        sccip.CommonChargeItemReference.Load();
                      
                       
                         tb_CostType CostTyp = db.tb_CostType.FirstOrDefault(p => p.CostTypeId == sccip.CostTypeId);

                         if (CostTyp != null)
                        {
                             //list.c
                            list.tb_CostTypeReference.Load();
                            info.CostType = new tb_CostType_info() { CostTypeId = CostTyp.CostTypeId, Remark = CostTyp.Remark, TypeName = CostTyp.TypeName };
                            info.CCII = new CommonChargeItem_info() { AreaPrice = list.AreaPrice, CommonChargeItemId = list.CommonChargeItemId, CostTypeId = list.tb_CostType.CostTypeId, ItemName = list.ItemName, SinglePrice = list.SinglePrice, UnitPrice = list.UnitPrice };

                        }
                        else
                        {
                            sccip.CommonChargeItem.tb_CostTypeReference.Load();
                            info.CostType = new tb_CostType_info() { CostTypeId = sccip.CommonChargeItem.tb_CostType.CostTypeId, Remark = sccip.CommonChargeItem.tb_CostType.Remark, TypeName = sccip.CommonChargeItem.tb_CostType.TypeName };
                            info.CCII = new CommonChargeItem_info() { AreaPrice = sccip.CommonChargeItem.AreaPrice, CommonChargeItemId = sccip.CommonChargeItem.CommonChargeItemId, CostTypeId = sccip.CommonChargeItem.tb_CostType.CostTypeId, ItemName = sccip.CommonChargeItem.ItemName, SinglePrice = sccip.CommonChargeItem.SinglePrice, UnitPrice = sccip.CommonChargeItem.UnitPrice };

                        }
                     
                        info.Shop = new Shop_New_Info() { ShopID = shop.ShopId, ShopName = shop.shopName };
                        info.SinglePrice = sccip.SinglePrice;
                        info.UnitPrice = sccip.UnitPrice;
                        info.TypeName = info.CostType.TypeName;
                        info.isShopCommomItem = info.isShopCommomItem2 = true;
                    }
                    else
                    {
                        list.tb_CostTypeReference.Load();
                        tb_CostType costType = list.tb_CostType;
                        info.AreaPrice = list.AreaPrice;

                        info.CCII = info.CCII = new CommonChargeItem_info() { AreaPrice = list.AreaPrice, CommonChargeItemId = list.CommonChargeItemId, CostTypeId = list.tb_CostType.CostTypeId, ItemName = list.ItemName, SinglePrice = list.SinglePrice, UnitPrice = list.UnitPrice }; ;
                        info.CostType = null;
                        info.Shop = new Shop_New_Info() { ShopID = shop.ShopId, ShopName = shop.shopName };
                        info.SinglePrice = list.SinglePrice;
                        info.UnitPrice = list.UnitPrice;
                       
                     
                        info.TypeName = costType.TypeName;
                        info.isShopCommomItem = info.isShopCommomItem2 = false;
                    }
                    rtnList.Add(info);


                }
 

                return rtnList;
            }
        }

        /// <summary>
        /// 更改商户基本收费类型
        /// </summary>
        /// <param name="inList"></param>
        /// <returns></returns>
        public int UpdateShopCommonChangeItemPriceInfo(List<Shop_CommonChargeItem_Price_Info> inList,int inShopId)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                foreach (Shop_CommonChargeItem_Price_Info itemPriceInfo in inList)
                {

                    //db.SaveChanges();
                    if (itemPriceInfo.isShopCommomItem && itemPriceInfo.isShopCommomItem2)//更新
                    {
                        Shop_CommonChargeItem_Price sccp = db.Shop_CommonChargeItem_Price.FirstOrDefault(p => p.ShopId == itemPriceInfo.Shop.ShopID && p.CommonChargeItemId == itemPriceInfo.CCII.CommonChargeItemId);
                        sccp.AreaPrice = itemPriceInfo.AreaPrice;
                        sccp.SinglePrice = itemPriceInfo.SinglePrice;
                        sccp.UnitPrice = itemPriceInfo.UnitPrice;
                        tb_CostType costType = db.tb_CostType.FirstOrDefault(p => p.TypeName == itemPriceInfo.TypeName);
                        sccp.CostTypeId = costType.CostTypeId;
                    }
                    else if (itemPriceInfo.isShopCommomItem && !itemPriceInfo.isShopCommomItem2)//新增
                    {
                        Shop shop = db.Shop.FirstOrDefault(p => p.ShopId == inShopId);
                         Shop_CommonChargeItem_Price sccp=new Shop_CommonChargeItem_Price();
                         sccp.Shop = shop;
                        sccp.SinglePrice = itemPriceInfo.SinglePrice;
                        sccp.UnitPrice = itemPriceInfo.UnitPrice;
                        sccp.AreaPrice = itemPriceInfo.AreaPrice;
                        sccp.CommonChargeItem =
                            db.CommonChargeItem.FirstOrDefault(
                                p => p.CommonChargeItemId == itemPriceInfo.CCII.CommonChargeItemId);
                         tb_CostType costType = db.tb_CostType.FirstOrDefault(p => p.TypeName == itemPriceInfo.TypeName);
                         sccp.CostTypeId = costType.CostTypeId;
                        shop.Shop_CommonChargeItem_Price.Load();

                        shop.Shop_CommonChargeItem_Price.Add(sccp);

                    }else if(!itemPriceInfo.isShopCommomItem && itemPriceInfo.isShopCommomItem2)//删除
                    {
                        Shop shop = db.Shop.FirstOrDefault(p => p.ShopId == inShopId);
                        Shop_CommonChargeItem_Price sccp = db.Shop_CommonChargeItem_Price.FirstOrDefault(p => p.ShopId == itemPriceInfo.Shop.ShopID && p.CommonChargeItemId == itemPriceInfo.CCII.CommonChargeItemId);
                        shop.Shop_CommonChargeItem_Price.Load();
                        shop.Shop_CommonChargeItem_Price.Remove(sccp);
                    }
                    else
                    {
                        
                    }
                    db.SaveChanges();
                }

           
                return 0;
            }
        }

        /// <summary>
        ///   取得所有的类型
        /// </summary>
        /// <returns></returns>
        public List<tb_CostType_info> GetAllCostType()
        {
            List<tb_CostType_info> ListType = new List<tb_CostType_info>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<tb_CostType> list = db.tb_CostType.ToList();

                foreach (tb_CostType type in list)
                {
                    tb_CostType_info info = new tb_CostType_info();
                    info.CostTypeId = type.CostTypeId;
                    info.TypeName = type.TypeName;
                    ListType.Add(info);
                }
                return ListType;
            }
        }
        /// <summary>
        /// 获取产品列表
        /// </summary>
        /// <param name="inShopid"></param>
        /// <returns></returns>
        public List<ProductDDO> GetAllShopProducts(int inShopid)
        {
            List<ProductDDO> rtnList = new List<ProductDDO>();
            #region//Entity模式
            //using (newSampleYPDBEntities db=new newSampleYPDBEntities())
            //{
            //    List<Tb_Product> listAllProduct = db.Tb_Product.ToList();
            //    foreach (Tb_Product product in listAllProduct)
            //    {
            //        ProductDDO info = new ProductDDO();
            //        Shop_Product_Price shopProductPrice =
            //            db.Shop_Product_Price.FirstOrDefault(
            //                p => p.ShopId == inShopid && p.ProductId == product.ProuctId);
            //        if (shopProductPrice != null)
            //        {

            //            if (!shopProductPrice.tb_CostTypeReference.IsLoaded)
            //            {
            //                shopProductPrice.tb_CostTypeReference.Load();
            //            }

            //            info.AppendPrice = shopProductPrice.AppendPrice;
            //            info.CostTypeName = shopProductPrice.tb_CostType.TypeName;
            //            info.IsShopProduct = true;
            //        }
            //        else
            //        {
            //            info.IsShopProduct = false;

            //        }
            //        info.Height = product.Height;
            //        info.MnemonicCode = product.mnemonicCode;
            //        info.PageNum = product.PageNum;
            //        //info.ProductCategoryId=product.p
            //        info.ProductCode = product.ProductCode;
            //        info.ProductDes = product.ProductDes;
            //        info.ProductName = product.ProductName;
            //        info.ProuctId = product.ProuctId;
            //        if (!product.tb_ProductTypeReference.IsLoaded)
            //        {
            //            product.tb_ProductTypeReference.Load();
            //        }
            //        info.ProductTypeName = product.tb_ProductType.TypeName;
            //        if (!product.Tb_PorductCategoryReference.IsLoaded)
            //        {
            //            product.Tb_PorductCategoryReference.Load();
            //        }
            //        info.ProductCategoryId = product.Tb_PorductCategory.ProductCategoryId;
            //        info.ProductCategoryName = product.Tb_PorductCategory.ProductCategoryName;
            //        rtnList.Add(info);                  
            //}
            #endregion
            //MessageBox.Show("调用到bll下一步加载所有的产品");

            DataTable table = new DataTable();
            try
            {
                table = Shop_New_DAL.GetAllProductForSetPrice(inShopid);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            //MessageBox.Show("获取到商户产品列表" + table.Rows.Count);
            int intCount = table.Rows.Count;
            for (int i = 0; i < intCount; i++)
            {
                ProductDDO ddo = new ProductDDO();
                ddo.AppendPrice = Convert.ToDecimal(table.Rows[i]["AppendPrice"]);
                ddo.CostTypeName = Convert.ToString(table.Rows[i]["CostTypeName"]);
                ddo.Height = Convert.ToDouble(table.Rows[i]["Height"]);
                ddo.IsShopProduct2 = ddo.IsShopProduct = Convert.ToBoolean(table.Rows[i]["IsShopProduct"]);

                ddo.MnemonicCode = Convert.ToString(table.Rows[i]["MnemonicCode"]);

                ddo.PageNum = Convert.ToInt32(table.Rows[i]["PageNum"]);
                ddo.UnitPrice = Convert.ToDecimal(table.Rows[i]["UnitPrice"]);
                ddo.ProductCategoryName = Convert.ToString(table.Rows[i]["ProductCategoryName"]);
                ddo.ProductCode = Convert.ToString(table.Rows[i]["ProductCode"]);
                ddo.ProductDes = Convert.ToString(table.Rows[i]["ProductDes"]);
                ddo.ProductName = Convert.ToString(table.Rows[i]["ProductName"]);
                ddo.ProductTypeName = Convert.ToString(table.Rows[i]["ProductTypeName"]);
                ddo.ProuctId = Convert.ToInt32(table.Rows[i]["ProductId"]);
                ddo.SinglePrice = Convert.ToDecimal(table.Rows[i]["SinglePrice"]);
                ddo.Width = Convert.ToInt32(table.Rows[i]["Width"]);
                ddo.ShopId = inShopid;//更新时对比用

                rtnList.Add(ddo);

            }

            return rtnList;
            //}
        }
        /// <summary>
        /// 修改价格及计价方式
        /// </summary>
        /// <param name="inddo"></param>
        /// <param name="inShopID"></param>
        /// <returns></returns>
        public List<ProductDDO> UpdateShopProductPrice(List<ProductDDO> inddo, int inShopID)
        {
            List<ProductDDO> rtnddo = new List<ProductDDO>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                //List<ProductDDO> addDDO = inddo.Where(p => p.Status == EnumShopProductStatus.add).ToList();
                Shop shop = db.Shop.FirstOrDefault(p => p.ShopId == inShopID);
                foreach (ProductDDO ddo in inddo.Where(p => p.Status == 1))//只取有变化的项目
                {
                    tb_CostType costType = db.tb_CostType.FirstOrDefault(p => p.TypeName == ddo.CostTypeName);

                    if (costType == null)
                    {
                        costType = db.tb_CostType.FirstOrDefault();
                    }

                    if (ddo.IsShopProduct && ddo.IsShopProduct2)//此产品是商户旧产品说明价格有变化
                    {
                        //Tb_Product product = db.Tb_Product.FirstOrDefault(p => p.ProuctId == ddo.ProuctId);
                        Shop_Product_Price spp =
                            db.Shop_Product_Price.FirstOrDefault(
                                p => p.ShopId == inShopID && p.ProductId == ddo.ProuctId);
                        if (spp != null)
                        {
                            spp.AppendPrice = ddo.AppendPrice;
                            spp.SinglePrice = ddo.SinglePrice;
                            spp.UnitPrice = ddo.UnitPrice;
                            spp.tb_CostType = costType;
                        }
                        else
                        {
                            rtnddo.Add(ddo);//产品不属于该商户
                        }
                    }
                    else if (ddo.IsShopProduct && !ddo.IsShopProduct2)//新添加的产品
                    {
                        Shop_Product_Price spp = new Shop_Product_Price();
                        Tb_Product product = db.Tb_Product.FirstOrDefault(p => p.ProuctId == ddo.ProuctId);
                        spp.Shop = shop;
                        spp.Tb_Product = product;
                        spp.tb_CostType = costType;
                        spp.UnitPrice = ddo.UnitPrice;
                        spp.AppendPrice = ddo.AppendPrice;
                        spp.SinglePrice = ddo.SinglePrice;
                        db.AddToShop_Product_Price(spp);
                    }
                    else if (!ddo.IsShopProduct && ddo.IsShopProduct2)//删除的产品
                    {
                        Shop_Product_Price spp =
                           db.Shop_Product_Price.FirstOrDefault(
                               p => p.ShopId == inShopID && p.ProductId == ddo.ProuctId);
                        shop.Shop_Product_Price.Load();
                        shop.Shop_Product_Price.Remove(spp);
                    }
                    else//无变化
                    {

                    }
                    db.SaveChanges();
                }
                shop.ProductVersion += 0.1;
                db.SaveChanges();

            }
            return rtnddo;
        }
        /// <summary>
        /// 获取商户的货运信息
        /// </summary>
        /// <param name="inShopID"></param>
        /// <returns></returns>
        public shop_DistributionInfo_Info GetShopDistributionInfo(int inShopID)
        {
            shop_DistributionInfo_Info info=new shop_DistributionInfo_Info();
            using (newSampleYPDBEntities db=new newSampleYPDBEntities())
            {
                Shop shop = db.Shop.FirstOrDefault(p => p.ShopId == inShopID);
                shop.shop_DistributionInfoReference.Load();
                if(shop.shop_DistributionInfo==null)
                {
                    return new shop_DistributionInfo_Info();
                }
                shop_DistributionInfo shopDistributionInfo = shop.shop_DistributionInfo;
                info.BusTimetable = shopDistributionInfo.BusTimetable;
                info.ConsigneeName = shopDistributionInfo.ConsigneeName;
                info.ConsigneeTelePhone = shopDistributionInfo.ConsigneeTelePhone;
                info.DistributionAddress = shopDistributionInfo.DistributionAddress;
                info.DistributionMode = shopDistributionInfo.DistributionMode;
                info.FarePayer = shopDistributionInfo.FarePayer;
                info.ReMark = shopDistributionInfo.ReMark;
                info.ShopId = inShopID;
            }
            return info;
        }
        /// <summary>
        /// 更新商户信息
        /// </summary>
        /// <param name="inInfo"></param>
        /// <param name="inshopId"></param>
        /// <returns></returns>
        public int UpdateShopDistributionInfo( shop_DistributionInfo_Info inInfo,int inshopId)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                Shop shop = db.Shop.FirstOrDefault(p => p.ShopId == inshopId);
                shop.shop_DistributionInfoReference.Load();
                if (shop.shop_DistributionInfo != null)
                {
                    shop_DistributionInfo shopDistributionInfo = shop.shop_DistributionInfo;
                    shopDistributionInfo.BusTimetable = inInfo.BusTimetable;
                    shopDistributionInfo.ConsigneeName = inInfo.ConsigneeName;
                    shopDistributionInfo.ConsigneeTelePhone = inInfo.ConsigneeTelePhone;
                    shopDistributionInfo.DistributionAddress = inInfo.DistributionAddress;
                    shopDistributionInfo.DistributionMode = inInfo.DistributionMode;
                    shopDistributionInfo.FarePayer = inInfo.FarePayer;
                    shopDistributionInfo.ReMark = inInfo.ReMark;
                }
                else
                {
                    shop_DistributionInfo shopDistributionInfo = new shop_DistributionInfo();
                    shopDistributionInfo.BusTimetable = inInfo.BusTimetable;
                    shopDistributionInfo.ConsigneeName = inInfo.ConsigneeName;
                    shopDistributionInfo.ConsigneeTelePhone = inInfo.ConsigneeTelePhone;
                    shopDistributionInfo.DistributionAddress = inInfo.DistributionAddress;
                    shopDistributionInfo.DistributionMode = inInfo.DistributionMode;
                    shopDistributionInfo.FarePayer = inInfo.FarePayer;
                    shopDistributionInfo.ReMark = inInfo.ReMark;
                    shopDistributionInfo.Shop = shop;
                    db.AddToshop_DistributionInfo(shopDistributionInfo);
                }
                db.SaveChanges();
            }
            return 0;
        }
        /// <summary>
        /// 重置客户的认证码
        /// </summary>
        /// <param name="inShopId"></param>
        /// <returns></returns>
        public string  ResetShopShaCode(int inShopId)
        {
            string codeSrc = inShopId + DateTime.Now.ToString("yyyymmdd");
            string ShaCode = MD5HashCode.GetHash(codeSrc);
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                Shop shop = db.Shop.FirstOrDefault(sh => sh.ShopId == inShopId);
                if (shop == null)
                {
                    return "";

                }
                else
                {
                    shop.SHACODE = ShaCode;
                }
                db.SaveChanges();
            }
            return ShaCode;
        }
        /// <summary>
        /// 重置远程开单密码为0
        /// </summary>
        /// <param name="inShopId"></param>
        /// <returns></returns>
        public string ResetShopPwd(int inShopId)
        {
            string codeSrc = "0";
            string ShaCode = MD5HashCode.GetHash(codeSrc);
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                Shop shop = db.Shop.FirstOrDefault(sh => sh.ShopId == inShopId);
                if (shop == null)
                {
                    return "";

                }
                else
                {
                    shop.FtpLoginName = null;
                    shop.FtpLoginPWD = null;
                }
                db.SaveChanges();
            }
            return ShaCode;
        }
        public int SetForceCheck(int shopID, bool isCheck)
        {

            int rtnVal = 0;
            try
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    Shop shop = db.Shop.FirstOrDefault(pt => pt.ShopId == shopID);
                    shop.ForceCheck = isCheck;
                    db.SaveChanges();
                    rtnVal = 1;

                }
            }
            catch {
                rtnVal = -1;
            }
            return rtnVal;
        }
        #region help methods
        /// <summary>
        /// 从SHOPENTITY转到SHOP_new_Info
        /// </summary>
        /// <param name="shopList"></param>
        /// <returns></returns>
        private List<Shop_New_Info> ConvertShop_New_InfoFromShopEntity(List<Shop> shopList, newSampleYPDBEntities db )
        {
            List<Shop_New_Info> shopInfoList = new List<Shop_New_Info>();
            //shopList.ForEach(s => shopInfoList.Add(new Shop_New_Info()
            //{
            //    Addr = s.address,
            //    AreaName = s.Area,
            //    ContactPerson = s.shopKeeper,
            //    CreditLevel = s.CreditLevel ?? 0,
            //    Email = s.EmailAddr,
            //    MiniCode = s.mnemonicCode,
            //    IsPrintPrice = s.IsNeedPrintUPrice ?? false,
            //    MSN = s.MSN,
            //    MobilePhoneNum = s.Tel1,
            //    PaperFaceID = s.PaperFaceId ?? 0,
            //    PaperTypeID = s.PaperTypeId ?? 0,
            //    PhoneNum = s.Tel2,
            //    SalesMan = s.Salesman,
            //    Priority = s.Priority ?? 0,
            //    QQ = s.QQ,
            //    Remark = s.Remark,
            //    ShopID = s.ShopId,
            //    ShopName = s.shopName,
            //    Status = s.Status ?? 0,
            //    IsAssignByCustom = s.IsAssignByCustom,
            //    FtpLoginName=s.FtpLoginName,
            //    FtpLoginPWD=s.FtpLoginPWD,
            //    SHACODE=s.SHACODE,
            //    IsForceCheck=s.ForceCheck
                
                
            //}));
            foreach (Shop s in shopList)
            {
                s.shop_DistributionInfoReference.Load();
                shopInfoList.Add(new Shop_New_Info() {
                    Addr = s.address,
                    AreaName = s.Area,
                    ContactPerson = s.shopKeeper,
                    CreditLevel = s.CreditLevel ?? 0,
                    Email = s.EmailAddr,
                    MiniCode = s.mnemonicCode,
                    IsPrintPrice = s.IsNeedPrintUPrice ?? false,
                    MSN = s.MSN,
                    MobilePhoneNum = s.Tel1,
                    PaperFaceID = s.PaperFaceId ?? 0,
                    PaperTypeID = s.PaperTypeId ?? 0,
                    PhoneNum = s.Tel2,
                    SalesMan = s.Salesman,
                    Priority = s.Priority ?? 0,
                    QQ = s.QQ,
                    Remark = s.Remark,
                    ShopID = s.ShopId,
                    ShopName = s.shopName,
                    Status = s.Status ?? 0,
                    IsAssignByCustom = s.IsAssignByCustom,
                    FtpLoginName = s.FtpLoginName,
                    FtpLoginPWD = s.FtpLoginPWD,
                    SHACODE = s.SHACODE,
                    IsForceCheck = s.ForceCheck,
                     DistributionInfo=s.shop_DistributionInfo==null?"":s.shop_DistributionInfo.ConsigneeName,
                      LastPrintDate=s.LastPrintDate

                });
            }

            return shopInfoList;
        }




        #endregion
    }
}
